﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Rapor;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraReports.UI;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Du4
{
    public partial class Orders : XForm
    {
        public Orders()
        {
            InitializeComponent();
            TAM.DU4OrdersTableAdapter = new Datasets.MyDataTableAdapters.DU4OrdersTableAdapter();
        }

        RepositoryItemComboBox riYIKAMA
        {
            get
            {
                RepositoryItemComboBox ri = new RepositoryItemComboBox();
                ri.TextEditStyle = TextEditStyles.DisableTextEditor;
                ri.Items.AddRange(new object[] { "YIKAMALI", "YIKAMASIZ", "PARÇA BOYA" });
                return ri;
            }
        }

        BindingSource bs;
        DataTable dt = new DataTable();

        private void Orders_Load(object sender, EventArgs e)
        {

            this.Text = "Du4Orders";

            ds.DU4Orders.Columns["ToplamFiyat"].Expression = "GomlekFiyati + ISNULL(Upcharge,0)";
            //ds.DU4Orders.Columns["KarbelTermin"].Expression = "Termin-7";
            ds.DU4Orders.Columns.Add("Total", typeof(int), "ISNULL(E32, 0) + ISNULL(E34, 0) + ISNULL(E35, 0) + ISNULL(E36, 0) + ISNULL(E37, 0) + ISNULL(E38, 0) + ISNULL(E39, 0) + ISNULL(E40, 0) + ISNULL(E41, 0) + ISNULL(E42, 0) + ISNULL(E43, 0) + ISNULL(E44, 0) + ISNULL(E45, 0) + ISNULL(E46, 0) + ISNULL(E47, 0) + ISNULL(E48, 0)+ISNULL(E49, 0)+ ISNULL(E50, 0) + ISNULL(B32, 0) + ISNULL(B34, 0) + ISNULL(B36, 0) + ISNULL(B38, 0) + ISNULL(B40, 0) + ISNULL(B42, 0) + ISNULL(B44, 0) + ISNULL(B46, 0) + ISNULL(B48, 0)+ ISNULL(B50, 0)");
            ds.DU4Orders.Columns.Add("FabricOK", typeof(bool), "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).FabricOK)");
            ds.DU4Orders.Columns.Add("CutOrder", typeof(int), "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).CutOrder)");
            ds.DU4Orders.Columns.Add("Kritik", typeof(string), "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).Kritik)");
            ds.DU4Orders.Columns.Add("KesilenAdet", typeof(int));
            ds.DU4Orders.Columns.Add("YuklenenAdet", typeof(int));
            ds.DU4Orders.Columns.Add("YuklemeTarihi", typeof(DateTime));
            bs = new BindingSource(ds, "DU4Orders");
            AddBsToList(bs);
            InitGridView(gv, bs);


            gv.Columns["stUretimId"].Visible = ytk(127);
            if (ytk(125))
            {
                // Göksel Bey'e sadece görmek istediği kolonları gösteriyoruz.
                gv.MakeColumnInvisible("Scan", "stGenelId", "Durum", "stUretimId", "Kontrol", "PADate", "VerTarih", "KF", "DosyaAdi", "Season", "Termin", "KarbelTermin", "CustMatch", "SubstId", "Subst", "ProductId", "Fit", "Dugme", "DugmeRengi", "Contrast", "Contrast2", "Contrast3", "Subst1", "Subst2", "Subst3", "Product1Id", "Product2Id", "Product3Id", "Subst1Id", "Subst2Id", "Subst3Id", "OverDeyd", "Accessoires", "OzelNot", "InfoComments", "YikamaBilgisi", "CuttingGroup", "ProdPrice", "VendorId", "KumasFiyati", "GomlekFiyati", "Upcharge", "GonderiTarihi", "GonderiSekli", "KumasGelisi", "Barkod", "Yaka", "Kusak", "Poligot", "Manset1", "YakaCesidi", "PlanlamaLinesId", "ModifiedOn", "TopEni", "OKRId", "Linea");
            }

            gv.Columns["Kritik"].Fixed =
            gv.Columns["CutOrder"].Fixed =
            gv.Columns["FabricOK"].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            gv.Columns["YikamaBilgisi"].ColumnEdit = riYIKAMA;
            gv.Columns["Total"].VisibleIndex = gv.Columns["TotalPerPlan"].VisibleIndex;
            gv.YanYanaGetirSoluna("VendorId", "Total", "KesilenAdet", "YuklenenAdet", "YuklemeTarihi", "TopEni");
            
            gv.YanYanaGetir("MillArticleNumber:SubstId");
            gv.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            foreach (string str in "Season;OrderTarihi;CustMatch;DosyaAdi;TotalPerPlan;Article;ProdPrice".Split(';'))
                gv.Columns[str].Visible = false;

            gv.FormatAsNumber(2, "ProdPrice", "KumasFiyati", "GomlekFiyati", "Upcharge", "ToplamFiyat");
            gv.AddSummary("YuklenenAdet", "KesilenAdet", "Total", "E32", "E34", "E35", "E36", "E37", "E38", "E39", "E40", "E41", "E42", "E43", "E44", "E45", "E46", "E47", "E48", "E49", "E50", "B32", "B34", "B36", "B38", "B40", "B42", "B44", "B46", "B48", "E49", "B50");

            btnHesapla.Visibility = btnKaydet.Visibility = ytk(123) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            gv.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);

            btnHesapla.ItemClick += btnHesapla_ItemClick;

            foreach (DevExpress.XtraGrid.Columns.GridColumn col in gv.Columns)
                if (";Modell;ModellNo;ArticleNo;Colour;KesimNo;Durum;Etiket;Dugme;Fit;OzelNot;Blatt;PA;Kontrol;OzelNot;InfoComments;DugmeRengi".Contains(";" + col.FieldName + ";"))
                    col.OptionsFilter.FilterPopupMode = FilterPopupMode.CheckedList;

            gv.SetFilterTypeToCheckedList("Termin", "KarbelTermin", "VerTarih", "DugmeRengi", "GonderiTarihi", "DispoNo", "Contrast");
            gv.Columns["CutOrder"].OptionsFilter.ShowBlanksFilterItems = DevExpress.Utils.DefaultBoolean.True;

            GridManager gm = GridManager.GetManager(gv);
            gm.GridMenu.AddMenu("Stok Durumuna bak", StokDurumu_Click);
            gm.GridMenu.AddMenu("Kesim Föyü çıkar", KesimFoyu_Click);
            gm.GridMenu.AddMenu("Kesim Adedi gir", KesimAdedi_Click);
            gm.GridMenu.AddMenu("Ortak Kumaş Raporu", OrtakKumas_Click);
            gm.GridMenu.AddMenu("KesimNo İptal Et", CancelKesimNo);
            gm.GridMenu.AddMenu("Invoice", Invoice);

            gv.EnableRowDelete();

            gv.CellValueChanged += gv_CellValueChanged;

            btnGetir.Click += new EventHandler(btnGetir_Click);
            btnTemizle.Click += new EventHandler(btnTemizle_Click);

            CreateVerticalGrid();

            Rfrsh();
            CutOrder();

            dt = ds.DU4Orders.CopyToDataTable();


            this.KeyPreview = true;
            this.KeyUp += Orders_KeyUp;
        }

        void Orders_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.R)
            {
                btnGetir_Click(null, null);
                e.Handled = true;
            }
        }

        void btnHesapla_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnKaydet.PerformClick();
            GetDetails("DU4Orders", "GetDU4GomlekFiyat", 1);
            //LastUpdate = ((DateTime)ds.DU4Orders.Compute("MAX(ModifiedOn)", string.Empty));
            CutOrder();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int maxIdx = ds.DU4Orders.Select("idx=max(idx)")[0][0].ToInt(-1);
            string mailContentUpdateAdet = "";
            string mailContentUpdateTermin = "";
            string mailContentNewOrder = "";
            string mailSubject = "";
            string mailContent = "";
            string customerName = "DU4";

            try
            {
                foreach (DataRow dr in ds.DU4Orders.Select("idx is not null"))
                {
                    foreach (DataRow item in dt.Select("idx =" + dr["idx"] + " and Total <> " + dr["Total"].ToInt(-1)))
                    {
                        mailContentUpdateAdet += "<b>idx:</b> " + dr["idx"] + "<br />" +
                                                 "<b>Son Toplam:</b> " + dr["Total"] + "<br /><br />";
                    }
                }

                foreach (ConfExpert.Forms.Depo.Kauf.Orders.UpdatedCells item in this.items)
                    mailContentUpdateTermin += "<b>idx:</b> " + item.idx + "<br />" +
                                               "<b>Termin Tarihi:</b> " + item.termin + "<br /><br />";


                //ds.DU4Orders.Columns["KarbelTermin"].Expression = string.Empty;
                ds.DU4Orders.Columns["ToplamFiyat"].Expression =
                ds.DU4Orders.Columns["Total"].Expression =
                ds.DU4Orders.Columns["FabricOK"].Expression =
                ds.DU4Orders.Columns["CutOrder"].Expression =
                ds.DU4Orders.Columns["Kritik"].Expression = string.Empty;

                Save();
            }
            finally
            {
                ds.DU4Orders.Columns["ToplamFiyat"].Expression = "GomlekFiyati + ISNULL(Upcharge,0)";
                //ds.DU4Orders.Columns["KarbelTermin"].Expression = "Convert(datetime, Convert(int, Termin)-7)";
                ds.DU4Orders.Columns["Total"].Expression = "ISNULL(E32, 0) + ISNULL(E34, 0) + ISNULL(E35, 0) + ISNULL(E36, 0) + ISNULL(E37, 0) + ISNULL(E38, 0) + ISNULL(E39, 0) + ISNULL(E40, 0) + ISNULL(E41, 0) + ISNULL(E42, 0) + ISNULL(E43, 0) + ISNULL(E44, 0) + ISNULL(E45, 0) + ISNULL(E46, 0) + ISNULL(E47, 0) + ISNULL(E48, 0) +ISNULL(E49, 0)+ ISNULL(E50, 0)+ ISNULL(B32, 0) + ISNULL(B34, 0) + ISNULL(B36, 0) + ISNULL(B38, 0) + ISNULL(B40, 0) + ISNULL(B42, 0) + ISNULL(B44, 0) + ISNULL(B46, 0) + ISNULL(B48, 0)+ ISNULL(B50, 0)";
                ds.DU4Orders.Columns["FabricOK"].Expression = "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).FabricOK)";
                ds.DU4Orders.Columns["CutOrder"].Expression = "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).CutOrder)";
                ds.DU4Orders.Columns["Kritik"].Expression = "MAX(Child(FK_DU4OrdersCut_DU4Orders_idx).Kritik)";

                gv.RefreshData();
                gv.BestFitColumns();

                foreach (DataRow dr in ds.DU4Orders.Select("idx >" + maxIdx))
                    mailContentNewOrder += "<b>idx:</b> " + dr["idx"] + "<br /><br />";


                mailSubject = (mailContentNewOrder == "" ? "" : ">Yeni Sipariş Kaydı ") +
                              ((mailContentUpdateAdet == "") && (this.items.Count < 1) ? "" : ">Sipariş Kaydı Değişikliği");

                mailContent = (mailContentNewOrder == "" ? "" : "<b>YENİ SİPARİŞ KAYDI</b>" + "<br />" + mailContentNewOrder) +
                              (mailContentUpdateAdet == "" ? "" : "<b>ADET DEĞİŞİKLİĞİ</b>" + "<br />" + mailContentUpdateAdet) +
                              (mailContentUpdateTermin == "" ? "" : "<b>TERMİN TARİHİ DEĞİŞİKLİĞİ</b>" + "<br />" + mailContentUpdateTermin);


                if (mailSubject != "")
                {
                    mailContent += "<b>Müşteri:</b> " + customerName + "<br />";
                    Mail.Send("tulay.tuncay@karbel.com", mailSubject, mailContent, "", "");
                    this.items.Clear();
                }

                this.dt = ds.DU4Orders.CopyToDataTable();
            }

        }

        void CutOrder()
        {
            ds.DU4OrdersCut.Clear();
            GetDetails("DU4OrdersCut", "GetDU4OrdersCut");
        }

        void Invoice(object sender, EventArgs e)
        {
            int idx = gv.GetRowCellValue(gv.FocusedRowHandle, "idx").ToInt(-1);
            using (Depo.Kauf.KaufInvoice f = new Depo.Kauf.KaufInvoice(idx, 22))
            {
                f.ShowDialog();
            }
        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            if (btnKaydet.Visibility == DevExpress.XtraBars.BarItemVisibility.Always)
                if (ds.HasChanges())
                    if (XtraMessageBox.Show("Değişiklikleri kaydetmek ister misiniz?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                        btnKaydet.PerformClick();

            Rfrsh();
        }

        private void Rfrsh()
        {
            ds.DU4Orders.Clear();

            GetDetails("DU4Orders", "GetDU4OrdersFiltered",
                boolParse(GetVal("Aktif")),
                strParse(GetVal("KesimNo")),
                strParse(GetVal("Durum")),
                intParse(GetVal("stUretimId")),
                strParse(GetVal("Kontrol")),
                strParse(GetVal("KF")),
                strParse(GetVal("Season")),
                strParse(GetVal("Blatt")),
                intParse(GetVal("PA")),
                intParse(GetVal("OrderNo")),
                intParse(GetVal("CustomerNo")),
                strParse(GetVal("CustMatch")),
                strParse(GetVal("Etiket")),
                intParse(GetVal("ProductId")),
                strParse(GetVal("Article")),
                strParse(GetVal("ModellNo")),
                strParse(GetVal("Modell")),
                strParse(GetVal("Fit")),
                strParse(GetVal("OzelNot")),
                strParse(GetVal("InfoComments")),
                strParse(GetVal("Barkod")),
                strParse(GetVal("Linea"))
                //datetimeParse(GetVal("BaslangicTarihi")),
                //datetimeParse(GetVal("BitisTarihi")),

                );

            gv.BestFitColumns();

            Notify("Done.");
        }

        private void CreateVerticalGrid()
        {
            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("Aktif", typeof(bool)),
                AddItem("KesimNo", typeof(string)),
                AddItem("Durum", typeof(string)),
                AddItem("stUretimId", typeof(int), RI(Rp.stUretim)),
                AddItem("Kontrol", typeof(string)),
                AddItem("KF", typeof(string)),
                AddItem("Season", typeof(string)),
                AddItem("Blatt", typeof(string)),
                AddItem("PA", typeof(int)),
                AddItem("OrderNo", typeof(int)),
                AddItem("CustomerNo", typeof(int)),
                AddItem("CustMatch", typeof(string)),
                AddItem("Etiket", typeof(string)),
                AddItem("ProductId", typeof(int), RI(Rp.V_Kumas)),
                AddItem("Article", typeof(string)),
                AddItem("ModellNo", typeof(string)),
                AddItem("Modell", typeof(string)),
                AddItem("Fit", typeof(string)),
                AddItem("OzelNot", typeof(string)),
                AddItem("InfoComments", typeof(string)),
                AddItem("Barkod", typeof(string)),
                AddItem("Linea", typeof(string))
                //AddItem("BaslangicTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                //AddItem("BitisTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
            });

            vgc.Rows.GetRowByFieldName("OzelNot", true).Height = 60;

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            vgc.SetCellValue(vgc.Rows["Aktif"], 0, true);
            //vgc.SetCellValue(vgc.Rows["BaslangicTarihi"], 0, DateTime.Now.AddDays(-7));
            //vgc.SetCellValue(vgc.Rows["BitisTarihi"], 0, DateTime.Now);

            vgc.FocusedRow = vgc.Rows["Durum"];
            vgc.BestFit();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }




        private void CancelKesimNo(object sender, EventArgs e)
        {
            string idxs = string.Empty;
            idxs = GetIdx(idxs);

            btnKaydet.PerformClick();
            if (XtraMessageBox.Show("Seçtiğiniz satırların kesim numaraları iptal edilecek. Emin misiniz?", "Kritik!", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                return;
            GetDetails("Du4Orders", "DeleteKesimNo", idxs);
            CutOrder();
        }

        private void OrtakKumas_Click(object sender, EventArgs e)
        {
            string idxs = string.Empty;
            idxs = GetIdx(idxs);

            if (idxs != String.Empty)
            {
                DataRow[] dr = ds.DU4Orders.Select("idx IN (" + idxs + ") and ISNULL(OKRId,0)<>0");
                if (dr.Length > 0 && dr.Length < gv.GetSelectedRows().Length)
                {
                    XtraMessageBox.Show("Sectiğiniz satırların bir kısmının ortak kumaşı çıkarılmış. Lütfen seçtiğiniz satırları kontrol ediniz", "Critical");
                    return;
                }
                else if (dr.Length == gv.GetSelectedRows().Length)
                {
                    int countKesim = intParse(dr.CopyToDataTable().Compute("Count(OKRId)", "OKRId ='" + dr.CopyToDataTable().Rows[0]["OKRId"].ToString() + "'"));
                    if (countKesim == gv.GetSelectedRows().Length)
                    {
                        XtraMessageBox.Show("Seçtiğiniz dosyaların ortak kumaş raporu alınmış!");
                        return;
                    }
                    else
                    {
                        XtraMessageBox.Show("Seçtiğiniz dosyalar için farklı ortak kumaş raporları çıkarılmış.");
                        return;
                    }
                }

                btnKaydet.PerformClick();
                ConfExpert.Forms.Du4.xrOrtakKumas ortakKumas = new ConfExpert.Forms.Du4.xrOrtakKumas(idxs, WhoAmI);
                ortakKumas.PrintingSystem.EndPrint += new EventHandler(PrintingSystem_EndPrint);

                ortakKumas.ShowPreviewDialog();
            }
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            GetDetails("DU4Orders", "UpdatedDu4OKR", idxs);
        }

        private void KesimAdedi_Click(object sender, EventArgs e)
        {
            int idx = intParse(bs, "idx");
            if (idx > 0)
                using (BedenDagilimi b = new BedenDagilimi(idx))
                {
                    b.ShowDialog();
                    GetDetails("DU4Orders", "GetDU4OrdersByIdx", idx);
                }
        }

        //private void ArtikelAl_Click(object sender, EventArgs e)
        //{
        //    //string dosyalar = string.Empty;
        //    //dosyalar = GetIdx(dosyalar);

        //    //btnKaydet.PerformClick();
        //    //GetDetails("DU4Orders", "GetDU4GomlekFiyat2", 1);
        //    //LastUpdate = ((DateTime)ds.DU4Orders.Compute("MAX(ModifiedOn)", string.Empty));
        //}

        private void StokDurumu_Click(object sender, EventArgs e)
        {
            string idxs = string.Empty;
            idxs = GetIdx(idxs);

            if (idxs != String.Empty)
                AppSession.MainForm.ShowMdiChildForm(typeof(Tuketim), idxs);

            //using (Tuketim tk = new Tuketim(idxs))
            //{
            //    tk.ShowDialog();
            //}
        }

        string idxs = string.Empty;
        private void KesimFoyu_Click(object sender, EventArgs e)
        {
            idxs = string.Empty;
            idxs = GetIdx(idxs);

            if (idxs != String.Empty)
            {
                string idxEx = string.Empty;
                var EB = ExecuteScalar("GetBedenDagilim", idxs);

                DataRow[] dr = ds.DU4Orders.Select("idx IN (" + idxs + ") and ISNULL(KesimNo,'')<>''");
                DataRow[] dr2 = ds.DU4Orders.Select("idx IN (" + idxs + ") and ISNULL(ST,0)<>0");
                DataRow[] dr3 = ds.DU4Orders.Select("idx IN (" + idxs + ") and ModellNo is null");
                //if (dr.Length > 0)
                //{
                //    foreach (DataRow row in dr)
                //        idxEx+= row["idx"].ToString()+",";

                //    if (idxEx.Substring(idxEx.Length - 1, 1) == ",")
                //        idxEx = idxEx.Substring(0, idxEx.Length - 1);

                //    if (XtraMessageBox.Show(idxEx + " idx numaralı satırların kesimi yapılmış. Yine de kesim föyü çıkarmak ister misiniz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                //        return;
                //}
                if (dr3.Length > 0)
                {
                    XtraMessageBox.Show("Kesim Föyü basmak için  ModellNo alanını doldurunuz !");
                    return;
                }
                if (EB.ToString() == "x" || (dr2.Length > 0 && dr2.Length != gv.GetSelectedRows().Length))
                {
                    XtraMessageBox.Show("Seçilen modeller aynı türden değil. Sadece erkek veya sadece bayan modeller veya sadece mendil seçin.");
                    return;
                }
                if (dr.Length > 0 && dr.Length < gv.GetSelectedRows().Length)
                {
                    XtraMessageBox.Show("Sectiğiniz satırların bir kısmının kesimi yapılmış. Lütfen seçtiğiniz satırları kontrol ediniz", "Critical");
                    return;
                }
                else if (dr.Length == gv.GetSelectedRows().Length)
                {
                    int countKesim = Convert.ToInt16(dr.CopyToDataTable().Compute("Count(KesimNo)", "KesimNo ='" + dr.CopyToDataTable().Rows[0]["KesimNo"].ToString() + "'"));
                    if (countKesim == gv.GetSelectedRows().Length)
                    {
                        if (XtraMessageBox.Show("Seçtiğiniz satırların tamamının kesimi yapılmış. Yine de kesim föyü çıkarmak ister misiniz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                            return;
                    }
                    else
                    {
                        XtraMessageBox.Show("Seçtiğiniz satırlar farklı kesim numaraları içeriyor");
                        return;
                    }
                }
                else if (dr.Length == 0)
                {
                    DataTable dt = ds.DU4Orders.Select("idx IN (" + idxs + ")").CopyToDataTable();
                    int countEtk = Convert.ToInt16(dt.Compute("Count(Etiket)", "Etiket ='" + dt.Rows[0]["Etiket"].ToString() + "'"));
                    int countYikama = Convert.ToInt16(dt.Compute("Count(Modell)", "Modell ='" + dt.Rows[0]["Modell"].ToString() + "'"));
                    if (dt.Rows.Count > countEtk || dt.Rows.Count > countYikama)
                    {
                        string message = "Seçtiğiniz satırlarda";
                        message += dt.Rows.Count > countEtk ? " farklı etiket bilgileri" : "";
                        message += dt.Rows.Count > countEtk && dt.Rows.Count > countYikama ? " ve" : "";
                        message += dt.Rows.Count > countYikama ? " farklı model bilgileri" : "";
                        message += "  mevcut. Yine de devam etmek ister misiniz ?";

                        if (XtraMessageBox.Show(message, "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                            return;
                    }
                    btnKaydet.PerformClick();

                    using (KesimNoAl kn = new KesimNoAl(dt, idxs))
                    {
                        if (kn.ShowDialog() == DialogResult.Cancel)
                            return;
                        else
                        {
                            GetDetails("DU4Orders", "UpdatedDu4KesimNo", idxs);
                            CutOrder();
                        }
                    }
                }

                if (EB.ToString() == "E")
                {
                    xrDU4KesimFoyu barcode = new xrDU4KesimFoyu(idxs);
                    barcode.PrintingSystem.StartPrint += new DevExpress.XtraPrinting.PrintDocumentEventHandler(PrintingSystem_StartPrint);
                    barcode.ShowPreview();
                }
                else if (EB.ToString() == "B")
                {
                    xrDU4KesimFoyuBayan barcode = new xrDU4KesimFoyuBayan(idxs);
                    barcode.PrintingSystem.StartPrint += new DevExpress.XtraPrinting.PrintDocumentEventHandler(PrintingSystem_StartPrint);
                    barcode.ShowPreview();
                }

            }
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            GetDetails("DU4Orders", "UpdDU4KFBasildi", idxs);
            gv.RefreshData();

            //LastUpdate = ((DateTime)ds.DU4Orders.Compute("MAX(ModifiedOn)", string.Empty));
        }

        private string GetIdx(string idxs)
        {
            foreach (int rowHandle in gv.GetSelectedRows())
                if (rowHandle > -1)
                    idxs += gv.GetRowCellValue(rowHandle, "idx").ToString() + ",";

            if (idxs.Substring(idxs.Length - 1, 1) == ",")
                idxs = idxs.Substring(0, idxs.Length - 1);
            return idxs;
        }

        //DateTime LastUpdate = new DateTime(2000, 1, 1);

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName.Length == 3)
                switch (e.Column.FieldName.Substring(0, 1))
                {
                    case "E": e.Appearance.ForeColor = Color.Navy; break;
                    case "B": e.Appearance.ForeColor = Color.Crimson; break;
                    default: e.Appearance.ForeColor = Color.Black; break;
                }

        }

        List<ConfExpert.Forms.Depo.Kauf.Orders.UpdatedCells> items = new List<ConfExpert.Forms.Depo.Kauf.Orders.UpdatedCells>();

        private void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            gv.CellValueChanged -= gv_CellValueChanged;
            if (e.Column.FieldName == "Termin" && this.gv.GetRowCellValue(e.RowHandle, "idx").ToInt(-1) > 0)
            {
                items.Add(new ConfExpert.Forms.Depo.Kauf.Orders.UpdatedCells
                {
                    idx = this.gv.GetRowCellValue(e.RowHandle, "idx").ToStr(),
                    termin = this.gv.GetRowCellValue(e.RowHandle, "Termin").ToStr()
                });
            }

            gv.CellValueChanged += gv_CellValueChanged;
        }

    }
}
